package com.supermap.guangzhou.weifang.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;


import java.util.ArrayList;

/**
 * Created by xueqing wang
 * 实现可以向所加的fragment添加数据
 */

public  class ViewPagerAdapter extends FragmentPagerAdapter {
    private ArrayList<String> tabTitles;
    private ArrayList<FragmentInfo> fragmentInfos;
    private Context context;

    public ViewPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        this.context = context;
        fragmentInfos = new ArrayList<FragmentInfo>();
        tabTitles = new ArrayList<String>();
    }

    /**
     * 添加一页
     *
     * @param title
     * @param clazz
     * @param bundle
     */
    public void addPager(String title, Class<?> clazz, Bundle bundle) {
        tabTitles.add(title);
        fragmentInfos.add(new FragmentInfo(clazz, bundle));
    }

    @Override
    public Fragment getItem(int position) {
        FragmentInfo fragmentInfo = fragmentInfos.get(position);
        return Fragment.instantiate(context, fragmentInfo.getClazz().getName(), fragmentInfo.getBundle());
    }

    @Override
    public int getCount() {
        return tabTitles.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles.get(position);
    }
}
class FragmentInfo{
    private Class<?> clazz;
    private Bundle bundle;
    public FragmentInfo(Class<?> clazz, Bundle bundle) {
        super();
        this.clazz = clazz;
        this.bundle = bundle;
    }
    public Class<?> getClazz() {
        return clazz;
    }
    public void setClazz(Class<?> clazz) {
        this.clazz = clazz;
    }
    public Bundle getBundle() {
        return bundle;
    }
    public void setBundle(Bundle bundle) {
        this.bundle = bundle;
    }
}
